package product

import (
	session2 "OnlineRetailers/controllers/admin/session"
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/models/Admin"
	error2 "OnlineRetailers/models/errorDeal"
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"os"
)

type UpdateProductDetailed struct {
	beego.Controller
}

func (c *UpdateProductDetailed) UpdateProductDetailed() {

	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()

	var addr = "/Product/UpdateProductDetailed"
	var err error

	productDetailed := &Admin.ProductDetailed{}
	img := &Admin.ProductDetailed{}
	log := &error2.Log{}
	mes := &error2.Mes{}
	class := []*Admin.Classification{}

	o := orm.NewOrm()

	err = json.Unmarshal([]byte(c.GetString("product")), productDetailed)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	err = json.Unmarshal([]byte(c.GetString("img")), img)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	err = json.Unmarshal([]byte(c.GetString("class")), &class)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	err = session2.CheckSession(c.Ctx.Request.Header["Session"], 1)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	//fmt.Println(key)

	if img.PrimariesImage != "" {
		_ = os.Remove("." + productDetailed.PrimariesImage)
		productDetailed.PrimariesImage = img.PrimariesImage
	}
	if img.CarouselImage1 != "" {
		_ = os.Remove("." + productDetailed.CarouselImage1)
		productDetailed.CarouselImage1 = img.CarouselImage1
	}
	if img.CarouselImage2 != "" {
		_ = os.Remove("." + productDetailed.CarouselImage2)
		productDetailed.CarouselImage2 = img.CarouselImage2
	}
	if img.DetailedImage1 != "" {
		_ = os.Remove("." + productDetailed.DetailedImage1)
		productDetailed.DetailedImage1 = img.DetailedImage1
	}
	if img.DetailedImage2 != "" {
		_ = os.Remove("." + productDetailed.DetailedImage2)
		productDetailed.DetailedImage2 = img.DetailedImage2
	}
	if img.DetailedImage3 != "" {
		_ = os.Remove("." + productDetailed.DetailedImage3)
		productDetailed.DetailedImage3 = img.DetailedImage3
	}
	if img.DetailedImage4 != "" {
		_ = os.Remove("." + productDetailed.DetailedImage4)
		productDetailed.DetailedImage4 = img.DetailedImage4
	}
	if img.DetailedImage5 != "" {
		_ = os.Remove("." + productDetailed.DetailedImage5)
		productDetailed.DetailedImage5 = img.DetailedImage5
	}

	if img.DetailedImage6 != "" {
		_ = os.Remove("." + productDetailed.DetailedImage6)
		productDetailed.DetailedImage6 = img.DetailedImage6
	}

	if img.DetailedImage7 != "" {
		_ = os.Remove("." + productDetailed.DetailedImage7)
		productDetailed.DetailedImage7 = img.DetailedImage7
	}

	err = DeleteClassM2M(o, productDetailed.ProductId)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	productDetailed.Classification = class
	//fmt.Println(class)
	if len(productDetailed.Classification) != 0 {
		err = PostClassM2M(o, productDetailed)
		if err != nil {
			c.Data["json"] = errorDeal.ErrMess(log, "402", err, addr, err.Error())
			return
		}
	}
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "402", err, addr, err.Error())
		return
	}

	_, err = o.Update(productDetailed)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	mes.Mes = "success"
	mes.Code = "200"
	c.Data["json"] = mes
}
